package com.shuoyue.ycgk.ui.questionbank.search;

import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.HotSearch;
import com.shuoyue.ycgk.entity.QuestionSimple;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;


import java.util.List;

import io.reactivex.Observable;

public interface SearchContract {
    interface View extends BaseView {
        void searchResult(ListWithPage<QuestionSimple> isNext);

        void addCollectSuc(QuestionSimple questionSimple);

        void deleteCollectSuc(QuestionSimple questionSimple);

        void searchHotSuc(List<HotSearch> hots);
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void searchQuestion(String searchType, String title, int modelId, int page) {
            mView.clearNetWork();
            apply(model.searchQuestion(searchType, title, modelId, page))
                    .subscribe(new ApiSubscriber<Optional<ListWithPage<QuestionSimple>>>(mView, this, true) {
                        @Override
                        public void onNext(Optional<ListWithPage<QuestionSimple>> optional) {
                            super.onNext(optional);
                            mView.searchResult(optional.getIncludeNull());
                        }

                    });
        }

        public void saveCollect(QuestionSimple questionSimple) {
            apply(model.saveCollect(questionSimple.getId(), 1))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> dayTagetOptional) {
                            super.onNext(dayTagetOptional);
                            mView.addCollectSuc(questionSimple);
                        }
                    });
        }

        public void deleteCollection(QuestionSimple questionSimple) {
            apply(model.deleteCollection(questionSimple.getId(), 1))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> dayTagetOptional) {
                            super.onNext(dayTagetOptional);
                            mView.deleteCollectSuc(questionSimple);
                        }
                    });
        }

        public void searchHot( ) {
            apply(model.searHot(null, 1))
                    .subscribe(new ApiSubscriber<Optional<List<HotSearch>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<HotSearch>> optional) {
                            super.onNext(optional);
                            mView.searchHotSuc(optional.getIncludeNull());

                        }

                    });
        }

    }

    class Model extends BaseModel {
        Observable<BaseResult<ListWithPage<QuestionSimple>>> searchQuestion(String searchType, String title, int modelId, int page) {
            return RetrofitClient.getInstance().getApi().searchQuestion(searchType, title, modelId, page);
        }

        Observable<BaseResult<String>> saveCollect(int questionId, int type) {
            return RetrofitClient.getInstance().getApi().saveCollect(questionId, type);
        }

        Observable<BaseResult<String>> deleteCollection(int questionId, int type) {
            return RetrofitClient.getInstance().getApi().deleteCollection(questionId, type);
        }

        Observable<BaseResult<List<HotSearch>>> searHot(Integer modelId, int type) {
            return RetrofitClient.getInstance().getApi().searchHot(modelId, type);
        }
    }
}
